﻿using BigEgg.Framework.Foundation;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Xml.Serialization;

namespace FMStudio.Documents.ProjectDocument
{
    [Export(typeof(IDocument)), Export]
    public class ProjectDocument : Document
    {
        #region Fields
        private int formatVersion = 1;

        private PropertyGroup properties;
        private readonly ObservableCollection<ProjectItem> items;
        #endregion


        public ProjectDocument()
        {
            this.properties = new PropertyGroup()
            {
                Name = "NewProject",
                Description = "NewProject"
            };
            this.items = new ObservableCollection<ProjectItem>();

            AddWeakEventListener(this.properties, PropertiesGroupPropertyChanged);
            AddWeakEventListener(this.items, ItemsCollectionChanged);
        }


        #region Properties
        [XmlAttribute()]
        public int FormatVersion
        {
            get { return this.formatVersion; }
            set
            {
                this.formatVersion = value;
            }
        }

        [XmlElement(Order = 1)]
        public PropertyGroup Properties 
        { 
            get { return this.properties; } 
            set
            {
                if (this.properties != value)
                {
                    this.properties = value;
                    RaisePropertyChanged("Properties");
                }
            }
        }

        [XmlElement(Order = 2, IsNullable = false)]
        public ObservableCollection<ProjectItem> Items { get { return this.items; } }
        #endregion

        #region Methods
        #region Private Methods
        private void PropertiesGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.IsModified = true;
            RaisePropertyChanged("Properties");
        }

        private void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.IsModified = true;
        }
        #endregion
        #endregion


        #region Nest Classes
        public class PropertyGroup : Model
        {
            #region Fields
            private string name = string.Empty;
            private string description = string.Empty;
            #endregion


            #region Properties
            public string Name
            {
                get { return this.name; }
                set
                {
                    if (this.name != value)
                    {
                        this.name = value;
                        RaisePropertyChanged("Name");
                    }
                }
            }

            public string Description
            {
                get { return this.description; }
                set
                {
                    if (this.description != value)
                    {
                        this.description = value;
                        RaisePropertyChanged("Description");
                    }
                }
            }
            #endregion
        }
        #endregion
    }
}
